package com.zw.mes.module.shipments.service;


import com.zw.mes.entity.asse.HalfStore;
import com.zw.mes.entity.shipments.*;
import com.zw.mes.module.prodtaskmanager.entity.VwProject;
import com.zw.mes.module.shipments.mapper.InstShipmentsMapper;
import com.zw.mes.module.shipments.mapper.InstShipmentsPlanMapper;
import com.zw.mes.module.shipments.model.LinshiProductDetail;
import com.zw.mes.module.shipments.repository.*;
import com.zw.mes.module.system.service.LogService;
import com.zw.mes.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @Author: liqingyu
 * @Date: 2020/11/10 15:11
 */
@Service
@SuppressWarnings("all")
public class InstShipmentsService {


    @Autowired
    private LogService logService;
    @Autowired
    private InstShipmentsMapper instShipmentsMapper;
    @Autowired
    private InstShipmentsRepository instShipmentsRepository;
    @Autowired
    private InstShipmentsDetailRepository instShipmentsDetailRepository;

    /**
     * 保存发货清单
     *
     * @param instShipmentsList
     * @param drawName
     * @param drawNo
     * @param proName
     * @param drawNum
     * @param unit
     * @param remark
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int saveAll(InstShipmentsList instShipmentsList, String[] drawName, String[] drawNo, String[] prodName, Integer[] drawNum, String[] unit, String[] remark) {
        instShipmentsList.setCreateName(ShiroUtils.getUser().getName());
        instShipmentsList.setCreateTime(new Date());
        instShipmentsList.setUpdateName(ShiroUtils.getUser().getName());
        instShipmentsList.setUpdateTime(new Date());
        instShipmentsList.setIsDel(0);
        instShipmentsList.setSubName(ShiroUtils.getUser().getName());
        instShipmentsList.setSubStatus(2);
        InstShipmentsList instShipmentsList1 = instShipmentsRepository.save(instShipmentsList);
        logService.saveLog("新增发货清单主单", "主单ID：" + instShipmentsList1.getId());
        for (int i = 0; i < drawName.length; i++) {
            InstShipmentsDetail instShipmentsDetail = new InstShipmentsDetail();
            instShipmentsDetail.setCreateTime(new Date());
            instShipmentsDetail.setDrawName(drawName[i]);
            instShipmentsDetail.setDrawNo(drawNo[i]);
            instShipmentsDetail.setDrawNum(drawNum[i]);
            instShipmentsDetail.setIsDel(0);
            instShipmentsDetail.setProdName(prodName[i]);
            instShipmentsDetail.setRemark(remark[i]);
            instShipmentsDetail.setSheetId(instShipmentsList1.getId());
            instShipmentsDetail.setUnit(unit[i]);
            InstShipmentsDetail instShipmentsDetail1 = instShipmentsDetailRepository.save(instShipmentsDetail);
            logService.saveLog("新增发货清单明细单", "明细单ID：" + instShipmentsDetail1.getId());
        }
        return 1;
    }

    /**
     * 发货清单主单查询
     *
     * @param instShipmentsList
     * @return
     */
    public List<InstShipmentsList> selectInstShipmentsList(InstShipmentsList instShipmentsList) {
        return instShipmentsMapper.selectInstShipmentsList(instShipmentsList);
    }

    /**
     * 发货清单明细单查询by sheetId
     *
     * @param instShipmentsDetail
     * @return
     */
    public List<InstShipmentsDetail> selectInstShipmentsDetail(InstShipmentsDetail instShipmentsDetail) {
        return instShipmentsMapper.selectInstShipmentsDetail(instShipmentsDetail);
    }

    /**
     * 保存修改发货清单
     *
     * @param instShipmentsList
     * @param drawName
     * @param drawNo
     * @param prodName
     * @param drawNum
     * @param unit
     * @param remark
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int saveAllEdit(InstShipmentsList instShipmentsList, String[] drawName, String[] drawNo, String[] prodName, Integer[] drawNum, String[] unit, String[] remark) {
        InstShipmentsList instShipmentsList11= instShipmentsRepository.getOne(instShipmentsList.getId());
        instShipmentsList11.setUpdateName(ShiroUtils.getUser().getName());
        instShipmentsList11.setUpdateTime(new Date());
        InstShipmentsList instShipmentsList1 = instShipmentsRepository.save(instShipmentsList11);
        logService.saveLog("修改发货清单", "主单ID：" + instShipmentsList1.getId());
        //删除明细 从新 添加
        int a = instShipmentsMapper.delInstShipmentsDetail(instShipmentsList1.getId());
        for (int i = 0; i < drawName.length; i++) {
            InstShipmentsDetail instShipmentsDetail = new InstShipmentsDetail();
            instShipmentsDetail.setCreateTime(new Date());
            instShipmentsDetail.setDrawName(drawName[i]);
            instShipmentsDetail.setDrawNo(drawNo[i]);
            instShipmentsDetail.setDrawNum(drawNum[i]);
            instShipmentsDetail.setIsDel(0);
            instShipmentsDetail.setProdName(prodName[i]);
            instShipmentsDetail.setRemark(remark[i]);
            instShipmentsDetail.setSheetId(instShipmentsList1.getId());
            instShipmentsDetail.setUnit(unit[i]);
            InstShipmentsDetail instShipmentsDetail1 = instShipmentsDetailRepository.save(instShipmentsDetail);
        }
        return 1;
    }

    /**
     * 删除
     * @param instShipmentsList
     * @return
     */
    public int del(InstShipmentsList instShipmentsList) {
        InstShipmentsList instShipmentsList1 = instShipmentsRepository.getOne(instShipmentsList.getId());
        instShipmentsList1.setIsDel(1);
        instShipmentsRepository.save(instShipmentsList1);
        logService.saveLog("删除发货清单", "主单ID：" + instShipmentsList1.getId());
        return  1;
    }

    /**
     * 撤销提交
     * @param instShipmentsList
     * @return
     */
    public int updateStaus(InstShipmentsList instShipmentsList) {
        InstShipmentsList instShipmentsList1 = instShipmentsRepository.getOne(instShipmentsList.getId());
        instShipmentsList1.setSubStatus(2);
        instShipmentsRepository.save(instShipmentsList1);
        logService.saveLog("撤销提交发货清单", "主单ID：" + instShipmentsList1.getId());
        return  1;
    }

    /**
     * 提交
     * @param instShipmentsList
     * @return
     */
    public int submit(InstShipmentsList instShipmentsList) {
        InstShipmentsList instShipmentsList1 = instShipmentsRepository.getOne(instShipmentsList.getId());
        instShipmentsList1.setSubStatus(1);
        instShipmentsRepository.save(instShipmentsList1);
        logService.saveLog("提交发货清单", "主单ID：" + instShipmentsList1.getId());
        return  1;
    }

    public int selectShipmentsComitCount() {
        return  instShipmentsMapper.selectShipmentsComitCount();
    }
}
